﻿using System;
using System.Collections.Generic;
using System.Text;
using Antu.Business.AbstractClass;
using System.Data;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using Antu.Business.Business;

namespace Antu.Business.Inheritance
{
    /// <summary>
    /// 签名倒换
    /// </summary>
    public class ExportUserSign : AbstractDataOperation
    {

        #region 外网导出为临时文件---完成
        /// <summary>
        /// 外网导出为临时文件
        /// </summary>
        /// <returns></returns>
        public override DataSet WebToTempFile(string[] arrTables)
        {
            //获取数据的集合，主要是包含两张表ST_USER 和 ST_USER_SIGN表
            Database dbTemp = BasicOperate.CreateWebDataBase();
            DataSet dsReturn = new DataSet();

            string strSql = string.Empty;

            int maxValue = arrTables.Length;
            int curValue = 1;
            foreach (string strTable in arrTables)
            {
                Application.DoEvents();
                base.SetProgress(maxValue, curValue, "正在导出...当前表名：\"" + strTable + "\"");

                Application.DoEvents();
                strSql = string.Format(@"select * from {0} ",strTable);

                DataSet dsSource = dbTemp.ExecuteDataSet(CommandType.Text, strSql);
                DataTable dtCopy = dsSource.Tables[0].Copy();

                if (dtCopy.Rows.Count > 0)//有数据才导出
                {
                    dtCopy.TableName = strTable;
                    dsReturn.Tables.Add(dtCopy);
                }
                curValue++;
            }   

            return dsReturn;
        }
        #endregion

        #region 导入内网---完成
        /// <summary>
        /// 用户签名数据导入内网
        /// </summary>
        /// <param name="arrTables">表名的集合</param>
        /// <param name="dsSource">表的数据集合</param>
        public override void TempFileToInside(string[] arrTables, DataSet dsSource)
        {
            Database dbTemp = BasicOperate.CreateInsideDataBase();
            DbConnection conTemp = dbTemp.CreateConnection();

            //获取需要导入对象的所有表名
            List<string> lstNeedImportTables = ClsOracleSystemOperation.GetAllTablesName();

            //更新数据
            foreach (string strTable in arrTables)
            {
                if (dsSource.Tables.Contains(strTable))  //源数据中包含该表才操作
                {
                    DataTable dtSource = dsSource.Tables[strTable];
                    string strTempTable = dtSource.TableName.ToUpper();

                    if (lstNeedImportTables.Contains(strTempTable))
                    {
                        conTemp.Open();
                        DbTransaction dbTrans = conTemp.BeginTransaction(); //开始事务

                        string strKeyName = string.Empty;

                        if (strTempTable == "ST_USER")
                        {
                            if (dtSource.Columns.Contains("LOGIN_NAME"))
                            {
                                strKeyName = "LOGIN_NAME";                                
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (dtSource.Columns.Contains("USERID"))
                            {
                                strKeyName = "USERID";
                            }
                            else
                            {
                                continue;
                            }
                        }

                        string strKeyId = string.Empty;

                        try
                        {
                            int maxValue = dtSource.Rows.Count;
                            int curValue = 1;
                            
                            //行循环
                            foreach (DataRow drCopy in dtSource.Rows)
                            {
                                Application.DoEvents();
                                SetProgress(maxValue, curValue, "正在导入...当前表名：\"" + strTempTable + "\"");

                                strKeyId = drCopy[strKeyName].ToString();

                                Application.DoEvents();
                                
                                //求取指定的行
                                DataSet dsOneRow = ClsOracleSystemOperation.GetOneRowData(strTempTable, strKeyName, strKeyId);
                                DataTable dtTemp = dsOneRow.Tables[0];

                                //如果不存在就添加一行空的数据
                                if (dtTemp.Rows.Count == 0)
                                {
                                    dtTemp.Rows.Add(dtTemp.NewRow());
                                }
                                //只取第一行数据
                                DataRow drTemp = dtTemp.Rows[0];
                                if (strTempTable == "ST_USER_SIGN")
                                {
                                    //获取表各列类型
                                    DataTable dtColTypeInfo = ClsOracleSystemOperation.GetFieldsNameOfTable(dbTemp, "ST_USER_SIGN");
                                    drTemp = ClsFieldType.UpdateColumns(drTemp, drCopy, dtColTypeInfo);
                                }
                                else
                                {
                                    drTemp = ClsFieldType.UpdateColumns(drTemp, drCopy);
                                }
                                
                                if (drTemp.RowState != DataRowState.Unchanged)
                                {
                                    DbCommand insertCommand = GetInsertCommand(dbTemp, DatabaseEngineType.OracleClient,strTempTable);
                                    DbCommand updateCommand = GetUpdateCommand(dbTemp, DatabaseEngineType.OracleClient,strTempTable, strKeyName);
                                    dbTemp.UpdateDataSet(dsOneRow, strTempTable, insertCommand, updateCommand,null, dbTrans);
                                }
                                curValue++;
                            }
                            dbTrans.Commit();
                        }
                        catch( Exception ex )
                        {
                            dbTrans.Rollback();
                            throw new Exception("出错关键字:"+strKeyId+"\r\n"+ex.Message);
                        }
                        finally
                        {
                            conTemp.Close();
                        }
                    }
                }
            }
        }
        #endregion

        #region 内网导出成临时文件---完成
        /// <summary>
        /// 内网导出成临时文件
        /// </summary>
        /// <param name="arrTables"></param>
        /// <returns></returns>
        public override DataSet InsideToTempFile(string[] arrTables)
        {
            //获取数据的集合，主要是包含两张表ST_USER 和 ST_USER_SIGN表
            Database dbTemp = BasicOperate.CreateInsideDataBase();
            DataSet dsReturn = new DataSet();

            string strSql = string.Empty;

            int maxValue = arrTables.Length;
            int curValue = 1;
            foreach (string strTable in arrTables)
            {
                Application.DoEvents();
                base.SetProgress(maxValue, curValue, "正在导出...当前表名：\"" + strTable + "\"");

                Application.DoEvents();
                strSql = string.Format(@"select * from {0} ", strTable);

                DataSet dsSource = dbTemp.ExecuteDataSet(CommandType.Text, strSql);
                DataTable dtCopy = dsSource.Tables[0].Copy();

                if (dtCopy.Rows.Count > 0)//有数据才导出
                {
                    dtCopy.TableName = strTable;
                    dsReturn.Tables.Add(dtCopy);
                }
                curValue++;
            }

            return dsReturn;
        }
        #endregion

        #region 临时文件到外网
        /// <summary>
        /// 临时文件到外网
        /// </summary>
        /// <param name="arrTables"></param>
        /// <param name="dsSource"></param>
        public override void TempFileToWeb(string[] arrTables, DataSet dsSource)
        {
            Database dbTemp = BasicOperate.CreateWebDataBase();
            DbConnection conTemp = dbTemp.CreateConnection();

            //获取需要导入对象的所有表名
            List<string> lstNeedImportTables = ClsOracleSystemOperation.GetAllTablesName();

            //更新数据
            foreach (string strTable in arrTables)
            {
                if (dsSource.Tables.Contains(strTable))  //源数据中包含该表才操作
                {
                    DataTable dtSource = dsSource.Tables[strTable];
                    string strTempTable = dtSource.TableName.ToUpper();

                    if (lstNeedImportTables.Contains(strTempTable))
                    {
                        conTemp.Open();
                        DbTransaction dbTrans = conTemp.BeginTransaction(); //开始事务

                        string strKeyName = string.Empty;

                        if (strTempTable == "ST_USER")
                        {
                            if (dtSource.Columns.Contains("LOGIN_NAME"))
                            {
                                strKeyName = "LOGIN_NAME";
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (dtSource.Columns.Contains("USERID"))
                            {
                                strKeyName = "USERID";
                            }
                            else
                            {
                                continue;
                            }
                        }

                        string strKeyId = string.Empty;
                        try
                        {
                            int maxValue = dtSource.Rows.Count;
                            int curValue = 1;

                            //行循环
                            foreach (DataRow drCopy in dtSource.Rows)
                            {
                                Application.DoEvents();
                                SetProgress(maxValue, curValue, "正在导入...当前表名：\"" + strTempTable + "\"");

                                strKeyId = drCopy[strKeyName].ToString();

                                Application.DoEvents();

                                //求取指定的行
                                DataSet dsOneRow = ClsOracleSystemOperation.GetOneRowData(strTempTable, strKeyName, strKeyId);
                                DataTable dtTemp = dsOneRow.Tables[0];

                                //如果不存在就添加一行空的数据
                                if (dtTemp.Rows.Count == 0)
                                {
                                    dtTemp.Rows.Add(dtTemp.NewRow());
                                }
                                //只取第一行数据
                                DataRow drTemp = dtTemp.Rows[0];
                                if (strTempTable == "ST_USER_SIGN")
                                {
                                    //获取表各列类型
                                    DataTable dtColTypeInfo = ClsOracleSystemOperation.GetFieldsNameOfTable(dbTemp, "ST_USER_SIGN");
                                    drTemp = ClsFieldType.UpdateColumns(drTemp, drCopy, dtColTypeInfo);
                                }
                                else
                                {
                                    drTemp = ClsFieldType.UpdateColumns(drTemp, drCopy);
                                }

                                if (drTemp.RowState != DataRowState.Unchanged)
                                {
                                    DbCommand insertCommand = GetInsertCommand(dbTemp, DatabaseEngineType.OracleClient, strTempTable);
                                    DbCommand updateCommand = GetUpdateCommand(dbTemp, DatabaseEngineType.OracleClient, strTempTable, strKeyName);
                                    dbTemp.UpdateDataSet(dsOneRow, strTempTable, insertCommand, updateCommand, null, dbTrans);
                                }
                                curValue++;
                            }
                            dbTrans.Commit();
                        }
                        catch( Exception ex)
                        {                          
                            dbTrans.Rollback();
                            throw new Exception("出错关键字:" + strKeyId + "\r\n" + ex.Message);
                        }
                        finally
                        {
                            conTemp.Close();
                        }
                    }
                }
            }
        }
        #endregion

    }
}
